/*
 * MyMap.cpp
 *
 *  Created on: 16 Oct 2013
 *      Author: Patrick
 */

#include <OgreConfigFile.h>
#include <OgreVector3.h>
#include <OgreImage.h>
#include <OgreSceneManager.h>
#include <OgreManualObject.h>

#include <engine/algorithms/ComputeHeightLines.h>
#include <engine/algorithms/ComputeSurface.h>

#include "Map.h"

using namespace Ogre;

namespace GameMap {

Map::Map(MyInputManager* inptMgr) {

	inptMgr->registerKeyListener(this);

	Ogre::ConfigFile cf;
	cf.load("game.cfg");

	std::string s_heightmap = cf.getSetting("HeightMap", "Map");
	this->heightmap = new Image();
	this->heightmap->load(s_heightmap,
			Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

	this->surface = Engine::ComputeSurface::computeSurface(this->heightmap, 10000);
	this->heightLines = Engine::ComputeHeightLines::computeHeightlines(this->surface, 0, 10000, 100);
}

Map::~Map() {
}

void Map::setupDemoScene(Ogre::SceneManager* sceneMgr) {

	this->setupHeightLines(sceneMgr);
	this->setupGrid(sceneMgr);
}

bool Map::keyPressed(const OIS::KeyEvent &arg) {

	switch(arg.key) {
	case OIS::KC_H:
		this->heightLinesNode->flipVisibility();
		break;
	case OIS::KC_G:
		this->gridNode->flipVisibility();
		break;
	default:
		break;
	}

	return true;
}

bool Map::keyReleased(const OIS::KeyEvent &arg) {
	return true;
}

void Map::setupHeightLines(Ogre::SceneManager* sceneMgr) {
	ColourValue colour = ColourValue(1, 1, 1);

	this->heightLinesNode = sceneMgr->createSceneNode("heightLinesNode");
	sceneMgr->getRootSceneNode()->addChild(this->heightLinesNode);

	for(unsigned int i = 0; i < this->heightLines.size(); i++) {
		Engine::MapObject* object2 = this->heightLines[i];
		ManualObject* testObject2 = object2->getRenderObject(sceneMgr, colour);
		this->heightLinesNode->attachObject(testObject2);
	}

	this->heightLinesNode->scale(200, 200, 1);
}

void Map::setupGrid(Ogre::SceneManager* sceneMgr) {
	ColourValue colour = ColourValue(1, 1, 1);

	this->gridNode = sceneMgr->createSceneNode("gridNode");
	sceneMgr->getRootSceneNode()->addChild(this->gridNode);

	ManualObject* grid = Engine::ComputeSurface::renderSurface(this->surface, sceneMgr, colour);
	this->gridNode->attachObject(grid);

	this->gridNode->scale(200, 200, 1);
}

}
